const mysql = require('mysql');
const config = require('./dbconfig.js');

var pool = mysql.createPool({
    host: config.database.HOST,
    user: config.database.USERNAME,
    password: config.database.PASSWORD,
    database: config.database.DATABASE
});

let query = function (sql, values) {

    return new Promise((resolve, reject) => {
        pool.getConnection(function (err, connection) {
            if (err) {
                resolve(err)
            } else {
                connection.query(sql, values, (err, rows) => {

                    if (err) {
                        reject(err)
                    } else {
                        resolve(rows)
                    }
                    connection.release()
                })
            }
        })
    })

};

// 查询test数据库
let findAllPost = function () {
    let _sql = `SELECT * FROM test`;
    return query(_sql)
};


module.exports = {
    findAllPost
};
